package com.ruoyi.manage.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.manage.domain.WareHouse;
import com.ruoyi.manage.mapper.WareHouseMapper;
import com.ruoyi.manage.service.WareHouseService;
import com.ruoyi.manage.threadLocal.UserThreadLocal;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * @author 满杯百香果🍎
 */
@Service
public class WareHouseServiceImpl extends ServiceImpl<WareHouseMapper, WareHouse> implements WareHouseService {

    @Autowired
    private WareHouseMapper wareHouseMapper;

    @Override
    public List<WareHouse> getList(WareHouse wareHouse) {
        String username = UserThreadLocal.getUsername();
        if (username.equals("admin")){
            return wareHouseMapper.getListByAdmin(wareHouse);
        }
        return wareHouseMapper.getList(username,wareHouse);
    }

    @Override
    public List<WareHouse> getMechanismName() {
        String username = UserThreadLocal.getUsername();
        if (username.equals("admin")){
            return wareHouseMapper.getMechanismNameByAdmin();
        }
        return wareHouseMapper.getMechanismName(username);
    }

    @Override
    public AjaxResult add(WareHouse wareHouse) {
        wareHouse.setWarehouseCode(upperCase("CK",6));
        if (wareHouseMapper.add(wareHouse) > 0){
            return AjaxResult.success("添加成功！");
        }
        return AjaxResult.error("添加失败！");
    }

    @Override
    public AjaxResult del(Integer id) {
        Integer i = wareHouseMapper.getGoodsCount(id);
        if (i == 0 && wareHouseMapper.del(id) > 0){
            return AjaxResult.success("删除成功！");
        }
        return AjaxResult.error("删除失败！可能是因为仓库中还有商品");
    }

    @Override
    public Integer together(List<WareHouse> list) {
        Integer wareHouseHasGoodsNum = 0;
        Integer wareHouseHasGoodsIndex = -1;
        // 没有商品的仓库需要更合并给有商品的仓库
        List<String> wareHouseCodes = new ArrayList<>();
        List<Long> wareHouseIds = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (wareHouseMapper.getGoodsCount(Integer.parseInt(list.get(i).getId().toString())) > 0){
                wareHouseHasGoodsIndex = i;
                wareHouseHasGoodsNum++;
                if (wareHouseHasGoodsNum >= 2){
                    return 0;
                }
            }
            if (i != wareHouseHasGoodsIndex){
                wareHouseCodes.add(list.get(i).getWarehouseCode());
                wareHouseIds.add(list.get(i).getId());
            }
        }
        Integer rows =wareHouseMapper.updateEmpOfWarehouseCode(wareHouseCodes,list.get(wareHouseHasGoodsIndex).getWarehouseCode());
        wareHouseMapper.deleteBatchIds(wareHouseIds);
        return 1;
    }

    @Test
    public void CK(){
        System.out.println(upperCase("CK", 6));
        System.out.println("ZK" + System.currentTimeMillis());
    }
    public String upperCase(String prefix, Integer i){
        StringBuilder upperCase = new StringBuilder(prefix);
        for (int j = 0; j < i; j++) {
            upperCase.append((char) (Math.random() * 26 + 'A'));
        }
        return upperCase.toString();
    }

}
